package com.yanqu.road.task;

import com.yanqu.road.connect.AgentServerLinkControl;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.protocol.AgentProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class RouteRule {

    private static Logger logger = LogManager.getLogger(RouteRule.class.getName());

    public static void route(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) {
        try {
            int code = yanQuMessage.getCode();
            if (code == CrossProtocol.C_CROSS_SERVICE_HANDLER) {
                SyncMessage.Request request = SyncMessage.Request.parseFrom(yanQuMessage.getBytes());
                code = request.getCmdCode();
            }

            if (code == AgentProtocol.A_UNION_ACTIVITY_GROUP_TRANSFER) {
                // 活动跨服（商会分组活动）
                AgentServerLinkControl.routeUnionActivityCross(yanQuMessage);
            } else if (code == AgentProtocol.A_TRANSFER_TO_APPOINT_ACTIVITY) {
                // 活动跨服(矿场争夺战)+新榷场
                AgentServerLinkControl.routeAppointActivityCross(yanQuMessage);
            } else if (code > 0 && code < 10000) {
                AgentServerLinkControl.routeClient(yanQuMessage);
            } else if (code > 10000 && code < 20000) {
                AgentServerLinkControl.routAgent(ctx, yanQuMessage);
            } else if (code > 20000 && code < 30000) {
                AgentServerLinkControl.routeGame(yanQuMessage);
            } else if (code > 30000 && code < 40000) {
                //普通跨服 1
                AgentServerLinkControl.routeRankCross(yanQuMessage);
            } else if (code > 40000 && code < 50000) {
                //活动跨服
                AgentServerLinkControl.routeActivityCross(yanQuMessage);
            } else if (code > 50000 && code < 55000) {
                // 普通跨服 2
                AgentServerLinkControl.routeCenterCross(yanQuMessage);
            } else if (code > 55000 && code < 60000) {
                //活动跨服（跨地区专用）
                AgentServerLinkControl.routeChannelActivityCross(yanQuMessage);
            } else if (code > 65000 && code < 70000) {
                // 宴会跨服
                AgentServerLinkControl.routeBanquetCross(yanQuMessage);
            } else {
                logger.info("消息错误, message string: " + yanQuMessage);
            }
        } catch (Exception e) {
            logger.error("执行command失败, message string: {}" , yanQuMessage, e);
        }
    }


}
